package day03;





import org.apache.flink.api.common.state.MapState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;
import untils.ClickEvent;
import untils.ClickSource;



public class Example2 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env
                .addSource(new ClickSource())
                .keyBy(r -> r.username)
                .process(new KeyedProcessFunction<String, ClickEvent, String>() {

                    private MapState<String,Integer> urlCount ;
                    @Override
                    public void open(Configuration parameters) throws Exception {
                        urlCount = getRuntimeContext().getMapState(new MapStateDescriptor<String , Integer>(
                                "url",
                                Types.STRING,
                                Types.INT
                        ));
                    }

                    @Override
                    public void processElement(ClickEvent value, Context ctx, Collector<String> out) throws Exception {
                        if(!urlCount.contains(value.url)){
                            urlCount.put(value.url,1);
                        } else {
                            urlCount.put(value.url,urlCount.get(value.url) + 1);
                        }
                        StringBuilder result = new StringBuilder();
                        result.append(value.username + " => {\n");
                        for (String url : urlCount.keys()){
                            result.append(" " + url + " -> " + urlCount.get(url) + ",\n");
                        }
                        result.append("} \n");
                        out.collect(result.toString());

                    }
                })
                .print();

        env.execute();
    }
}
